package com.example.fruitdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fruitdemo.mapper.GoodMapper;
import com.example.fruitdemo.pojo.Good;
import com.example.fruitdemo.ruslet.R;
import com.example.fruitdemo.service.GoodService;
import com.example.fruitdemo.service.UserService;
import com.example.fruitdemo.util.Code;
import com.example.fruitdemo.util.GoodList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {

@Autowired
private GoodMapper goodMapper;
@Autowired
private UserService userService;
@Autowired
private StringRedisTemplate redisTemplate;

    @Override
    public R<Page> fruitpage(int page, int pageSize, int categoryid, int orderby) {
        Page pageinfo=new Page(page,pageSize);
        LambdaQueryWrapper<Good> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Good::getCreatetime);
        if(orderby==0) //降序
            queryWrapper.orderByDesc(Good::getSignalprice);
        if(orderby==1)//升序
            queryWrapper.orderByAsc(Good::getSignalprice);
        queryWrapper.eq(Good::getGoryid,categoryid);
        this.page(pageinfo,queryWrapper);

        return R.Ok(pageinfo);
    }

    @Override
    public R<Page> getbyname(String searchname,int page,int orderby) {
        //默认第一页,15条
        Page<Good> pageinfo=new Page<>(page,15);
        LambdaQueryWrapper<Good> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(searchname!=null,Good::getGoodname,searchname);
        queryWrapper.orderByDesc(Good::getCreatetime);
        if(orderby==0) //降序
           queryWrapper.orderByDesc(Good::getSignalprice);
        if(orderby==1)//升序
            queryWrapper.orderByAsc(Good::getSignalprice);
        Page<Good> goodpage = this.page(pageinfo, queryWrapper);
        return R.Ok(goodpage);
    }

    @Override
    public R<List<Good>> getnewgoods() {
        return R.Ok(goodMapper.newgoods());
    }

    /**
     * 最近浏览
     * @param authorization
     * @param goodid
     * @return
     */
    @Override
    public R<String> presentlook(String authorization, String goodid) {
        Long userid = userService.getidByAuth(authorization);
         //查询商品
        long _goodid = Long.parseLong(goodid);
        Good good = this.getById(_goodid);
        if(redisTemplate.opsForValue().get(Code.USER_LOOK+userid)==null){
            //没浏览记录，新增
            GoodList goodList=new GoodList();
            List<Good> goods=new ArrayList<>();
            goods.add(good);
            goodList.setGoodlist(goods);
             redisTemplate.opsForValue().set(Code.USER_LOOK+userid, JSON.toJSONString(goodList));
        }if(redisTemplate.opsForValue().get(Code.USER_LOOK+userid)!=null||!redisTemplate.opsForValue().get(Code.USER_LOOK+userid).isEmpty()){
            //不为空，查询出来最多不超过五个
            String json = redisTemplate.opsForValue().get(Code.USER_LOOK + userid);
            GoodList goodList = JSONObject.parseObject(json, GoodList.class);
            List<Good> goodlist = goodList.getGoodlist();
            if(goodlist.size()<5){
                //遍历去重
                int index=-1;
                for(int i=0;i<goodlist.size();i++){
                    if(goodlist.get(i).getId()==_goodid){
                        index=i;
                    }
                }
                if(index!=-1){
                    goodlist.remove(index);
                }
                goodlist.add(good);
                //重新赋值集合
                goodList.setGoodlist(goodlist);
                redisTemplate.opsForValue().set(Code.USER_LOOK+userid,JSON.toJSONString(goodList));
            }else{
                //等于五，删除最后一个，再增加一个
                goodlist.remove(4);
                //遍历去重
                int index=-1;
                for(int i=0;i<goodlist.size();i++){
                    if(goodlist.get(i).getId()==_goodid){
                        index=i;
                    }
                }
                if(index!=-1){
                    goodlist.remove(index);
                }
                goodlist.add(good);
                //重新赋值集合
                goodList.setGoodlist(goodlist);
                //在重新写入
                redisTemplate.opsForValue().set(Code.USER_LOOK+userid,JSON.toJSONString(goodList));
            }
        }
       return R.Ok();
    }

    /**
     * 获取最近浏览记录
     * @param authorization
     * @return
     */
    @Override
    public R<Page> getPresent(String authorization) {
        Long userid = userService.getidByAuth(authorization);
        Page<Good> goodPage=new Page<>(1,5);
        String json = redisTemplate.opsForValue().get(Code.USER_LOOK + userid);
        GoodList goodList = JSONObject.parseObject(json, GoodList.class);
        List<Good> goodlist = goodList.getGoodlist();
        goodPage.setRecords(goodlist);
        return R.Ok(goodPage);
    }

}
